﻿using System;
using System.Web;
using System.Web.Security;
using ESN.Models;

namespace ESN.Helpers
{
    public class PermissionHelper
    {
        /*
         * All the function return TRUE or FALSE
         * isAdmin()/isSMod()/isMod() return TRUE if online user is Admin/Smod/Mod
         * isAdmin(string name)/isSMod(string name)/isMod(string name) return TRUE if the specific user by name is Admin/Smod/Mod
         * readAllExercises() return permission for QuizService
         * IsAuthenticated() return TRUE if user has logged in or the querystrung provide vailid user and pass parameter
         * IsAuthenticated(ref Guid id) do tha same, but give you the user online ID
         * IsOwner(string name) return TRUE if the online user is match with user name
         * IsOwner(Guid id) return TRUE if the online user is match with user id
         * isFriend(Guid id) return TRUE if the online user is freind of the user by id
         * readWall(string name) return TRUE if the online user can read some one wall given by thier UserName
         * readWall(Gui Id) return TRUE if the online user can read some one wall given by thier UserId
         * postWall(string name) return TRUE if the online user can post on some one wall given by thier UserName
         * postWall(Guid id) return TRUE if the online user can post on some one wall given by thier UserId
         * deleteWall(int WId) return TRUE if the online user can Delete o wall post
         */
        public static bool isAdmin()
        {
            if (Roles.IsUserInRole("Admin"))
                return  true;
            return false;
        }
        public static bool isSMod()
        {
            if (Roles.IsUserInRole("SMod"))
                return true;
            return false;
        }
        public static bool isMod()
        {
            if (Roles.IsUserInRole("Mod"))
                return true;
            return false;
        }

        public static bool isAdmin(string name)
        {
            if (Roles.IsUserInRole(name, "Admin"))
                return true;
            return false;
        }
        public static bool isSMod(string name)
        {
            if (Roles.IsUserInRole(name, "SMod"))
                return true;
            return false;
        }
        public static bool isMod(string name)
        {
            if (Roles.IsUserInRole(name, "Mod"))
                return true;
            return false;
        }
        //Quiz permision
        public static bool readAllExercises()
        {
            if (isAdmin() || isSMod() || isMod())
                return true;
            return false;
        }

        public static bool IsAuthenticated(ref Guid id)
        {
            var user = HttpContext.Current.Request.QueryString["user"];
            var pass = HttpContext.Current.Request.QueryString["pass"];
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                var onlineUser = Membership.GetUser();
                if (onlineUser == null)
                    return false;
                id = (Guid)onlineUser.ProviderUserKey;
                return true;
            }
            else if (Membership.ValidateUser(user, pass))
            {
                var onlineUser = Membership.GetUser(user);
                id = (Guid)onlineUser.ProviderUserKey;
                return true;
            }

            return false;
        }
        public static bool IsAuthenticated()
        {
            var user = HttpContext.Current.Request.QueryString["user"];
            var pass = HttpContext.Current.Request.QueryString["pass"];
            if ((HttpContext.Current.User.Identity.IsAuthenticated && Membership.GetUser() != null) || Membership.ValidateUser(user, pass))
                return true;

            return false;
        }

        public static bool IsOwner(string name)
        {
            if (!IsAuthenticated())
                return false;

            if(Membership.GetUser(name).ProviderUserKey.ToString() == Membership.GetUser().ProviderUserKey.ToString())
                return true;

            return false;
        }

        public static bool IsOwner(Guid id)
        {
            if (!IsAuthenticated())
                return false;

            if (id == (Guid)Membership.GetUser().ProviderUserKey)
                return true;

            return false;
        }

        public static bool IsOwner(int GId)
        {
            ESNCoreDbContext core = new ESNCoreDbContext();
            Guid id = new Guid();
            if (!IsAuthenticated(ref id))
                return false;

            var g = core.Groups.Find(GId);
            if (g == null)
                return false;

            if (g.OwnerId == id)
                return true;

            return false;
        }
        public static bool isFriend(Guid id)
        {
            if (!IsAuthenticated())
                return false;
            ESNCoreDbContext core = new ESNCoreDbContext();
            var online = core.Profiles.Find((Guid)Membership.GetUser().ProviderUserKey);
            var user = core.Profiles.Find(id);

            if (user.Friends.Contains(online))
                return true;
            return false;
        }
        public static bool isMember(int GId)
        {
            Guid id = new Guid();
            if (!IsAuthenticated(ref id))
                return false;
            ESNCoreDbContext core = new ESNCoreDbContext();
            var user = core.Profiles.Find(id);
            var g = core.Groups.Find(GId);
            if (g == null || user == null)
                return false;

            if (g.Members.Contains(user))
                return true;

            return false;
        }
        public static bool readWall(string name)
        {
            var owner = Membership.GetUser(name);
            if (owner == null)
                return false;
            ESNCoreDbContext core = new ESNCoreDbContext();
            var p = core.Profiles.Find((Guid)owner.ProviderUserKey);

            if (IsOwner(p.UserId))
                return true;

            if (p.isPrivate)
                return false;

            if (isFriend((Guid)owner.ProviderUserKey))
            {
                if (Char.GetNumericValue(p.WallPermission[0]) > 0)
                    return true;
            }
            else if (IsAuthenticated())
            {
                if (Char.GetNumericValue(p.WallPermission[1]) > 0)
                    return true;
            }
            else
            {
                if (Char.GetNumericValue(p.WallPermission[2]) > 0)
                    return true;
            }

            return false;
        }

        public static bool readWall(Guid id)
        {
            ESNCoreDbContext core = new ESNCoreDbContext();
            var p = core.Profiles.Find(id);

            if (IsOwner(p.UserId))
                return true;

            if (p.isPrivate)
                return false;

            if (isFriend(id))
            {
                if (Char.GetNumericValue(p.WallPermission[0]) > 0)
                    return true;
            }
            else if (IsAuthenticated())
            {
                if (Char.GetNumericValue(p.WallPermission[1]) > 0)
                    return true;
            }
            else
            {
                if (Char.GetNumericValue(p.WallPermission[2]) > 0)
                    return true;
            }

            return false;
        }
        public static bool readWall(int GId)
        {
            if (!IsOwner(GId))
                return true;

            ESNCoreDbContext core = new ESNCoreDbContext();
            Group g = core.Groups.Find(GId);

            if ((Char.GetNumericValue(g.WallPermission[0]) >= 0 && isMember(GId)) ||
                (Char.GetNumericValue(g.WallPermission[1]) >= 0 && IsAuthenticated()) ||
                Char.GetNumericValue(g.WallPermission[2]) >= 0)
                return true;

            return false;
        }
        public static bool postWall(string name)
        {
            var owner = Membership.GetUser(name);
            if (owner == null)
                return false;
            ESNCoreDbContext core = new ESNCoreDbContext();
            var p = core.Profiles.Find((Guid)owner.ProviderUserKey);

            if (IsOwner(p.UserId))
                return true;

            if ((Char.GetNumericValue(p.WallPermission[0]) >= 2 && isFriend((Guid)owner.ProviderUserKey)) ||
                (Char.GetNumericValue(p.WallPermission[1]) >= 2 && IsAuthenticated()) ||
                Char.GetNumericValue(p.WallPermission[2]) >= 2)
                return true;

            return false;
        }

        public static bool postWall(Guid id)
        {
            if (IsOwner(id))
                return true;

            ESNCoreDbContext core = new ESNCoreDbContext();
            var p = core.Profiles.Find(id);

            if ((Char.GetNumericValue(p.WallPermission[0]) >= 2 && isFriend(id)) ||
                (Char.GetNumericValue(p.WallPermission[1]) >= 2 && IsAuthenticated()) ||
                Char.GetNumericValue(p.WallPermission[2]) >= 2)
                return true;

            return false;
        }

        public static bool postWall(int GId)
        {
            if (!IsOwner(GId))
                return true;

            ESNCoreDbContext core = new ESNCoreDbContext();
            Group g = core.Groups.Find(GId);

            if ((Char.GetNumericValue(g.WallPermission[0]) >= 2 && isMember(GId)) ||
                (Char.GetNumericValue(g.WallPermission[1]) >= 2 && IsAuthenticated()) ||
                Char.GetNumericValue(g.WallPermission[2]) >= 2)
                return true;

            return false;
        }
        public static bool deleteWall(int WId)
        {
            ESNCoreDbContext core = new ESNCoreDbContext();
            var post = core.UserWalls.Find(WId);
            if (post == null)
                return false;

            if (IsOwner(post.WriterId))
                return true;

            return false;
        }

        static PermissionHelper()
        {
            if (!Roles.RoleExists("Admin"))
                Roles.CreateRole("Admin");
            if (!Roles.RoleExists("SMod"))
                Roles.CreateRole("SMod");
            if (!Roles.RoleExists("Mod"))
                Roles.CreateRole("Mod");
        }
    }
}